package cn.gson.springboot.controller;

import cn.gson.springboot.model.pojos.Customer;
import cn.gson.springboot.model.pojos.Employee;
import cn.gson.springboot.model.service.CustomerService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.sql.Array;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class CustomerController {
    @Autowired
    CustomerService customerService;
    @Autowired
    HttpSession session;

    //客户集合
    @RequestMapping("/customer-list")
    @ResponseBody
    public Map<String,Object> allCustomerList(Integer page, Integer size) {
        Page<Object> obj = PageHelper.startPage(page, size);
        List<Customer> cus = customerService.selectAllCustomer();
        for (Customer c:cus) {
            System.out.println(c);
        }
        Map<String,Object> map = new HashMap<>();
        map.put("total",obj.getTotal());
        map.put("rows",cus);
        return map;
    }

    //客户及合约信息集合
    @RequestMapping("/customer-contract-list")
    @ResponseBody
    public Map<String,Object> allCustomerAndContractList(Integer page, Integer size) {
        Page<Object> obj = PageHelper.startPage(page, size);
        List<Customer> cus = customerService.selectAllCustomerAndContract();
        for (Customer c:cus) {
            System.out.println(c);
        }
        Map<String,Object> map = new HashMap<>();
        map.put("total",obj.getTotal());
        map.put("rows",cus);
        return map;
    }



    //通过客户种类查询
    @RequestMapping("/customer-type-list")
    @ResponseBody
    public Map<String,Object> selectAllCustomerByType(Integer page, Integer size,Integer customerType) {
        Page<Object> obj = PageHelper.startPage(page, size);
        List<Customer> cus = customerService.selectAllCustomerByType(customerType);
        for (Customer c:cus) {
            System.out.println(c);
        }
        Map<String,Object> map = new HashMap<>();
        map.put("total",obj.getTotal());
        map.put("rows",cus);
        return map;
    }

    //查询对应登录员工ID的客户
    @RequestMapping("/customer-emp-list")
    @ResponseBody
    public Map<String,Object> selectAllCustomerByEmpId(Integer page, Integer size) {
        Page<Object> obj = PageHelper.startPage(page, size);
        Employee emp = (Employee) session.getAttribute("emp");
        List<Customer> cus = customerService.selectAllCustomerByEmpId(emp.getEmpId());
        for (Customer c:cus) {
            System.out.println(c);
        }
        Map<String,Object> map = new HashMap<>();
        map.put("total",obj.getTotal());
        map.put("rows",cus);
        return map;
    }

    //查询非对应登录员工的客户
    @RequestMapping("/customer-notemp-list")
    @ResponseBody
    public Map<String,Object> selectOtherCustomerByEmpId(Integer page, Integer size) {
        Page<Object> obj = PageHelper.startPage(page, size);
        Employee emp = (Employee) session.getAttribute("emp");
        List<Customer> cus = customerService.selectOtherCustomerByEmpId(emp.getEmpId());
        for (Customer c:cus) {
            System.out.println(c);
        }
        Map<String,Object> map = new HashMap<>();
        map.put("total",obj.getTotal());
        map.put("rows",cus);
        return map;
    }

    //客户名称OR简称模糊搜索
    @RequestMapping("/customer-search-list")
    @ResponseBody
    public Map<String,Object> selectCustomerBySearchValue(Integer page, Integer size, String searchValue){
        Page<Object> obj = PageHelper.startPage(page, size);
        List<Customer> cus = customerService.selectCustomerBySearchValue(searchValue);
        for (Customer c:cus) {
            System.out.println(c);
        }
        Map<String,Object> map = new HashMap<>();
        map.put("total",obj.getTotal());
        map.put("rows",cus);
        return map;
    }

    //员工客户集合
    @RequestMapping("/emp-all-customer-list")
    @ResponseBody
    public List<Customer>  CustomerByEmpAllList(HttpSession session) {
        Employee emp = (Employee) session.getAttribute("emp");
        List<Customer> cus = customerService.selectCustomerByEmp(emp.getDepartment().getDeptId(),null);
        return cus;
    }

    //员工客户集合分页
    @RequestMapping("/emp-customer-list")
    @ResponseBody
    public Map<String,Object> CustomerByEmpList(Integer page, Integer size,Integer type) {
        Page<Object> obj = PageHelper.startPage(page, size);
        Employee emp = (Employee) session.getAttribute("emp");
        List<Customer> cus = customerService.selectCustomerByEmp(emp.getDepartment().getDeptId(),type);
        Map<String,Object> map = new HashMap<>();
        map.put("total",obj.getTotal());
        map.put("rows",cus);
        return map;
    }

    //模糊查询客户
    @RequestMapping("/emp-fuzzy-customer-list")
    @ResponseBody
    public List<Customer> CustomerByFuzzyEmpList(String customerName,Integer type) {
        Employee emp = (Employee) session.getAttribute("emp");
        List<Customer> cus = customerService.selectCustomerByFuzzyEmp(emp.getDepartment().getDeptId(),customerName,type);
        return cus;
    }

    //根据客户生命周期查询所有数量
    @RequestMapping("/customer-lifecycle-number-all")
    @ResponseBody
    public List<Map<String,Object>> customerNumberByLifeCycle(HttpSession session) {
        Employee emp = (Employee) session.getAttribute("emp");
        List<Map<String,Object>> list = customerService.customerNumberByLifeCycle();
        System.out.println(list);
        return list;
    }

    //根据客户来源查询所有数量
    @RequestMapping("/customer-from-number-all")
    @ResponseBody
    public  List<Map<String,Object>> customerNumberByFrom(HttpSession session) {
        Employee emp = (Employee) session.getAttribute("emp");
        List<Map<String,Object>> list = customerService.customerNumberByFrom();
        System.out.println(list);
        return list;
    }

    //查询所有客户的合约统计
    @RequestMapping("/customer-contract-all")
    @ResponseBody
    public  List<Map<String,Object>> contractNumberByAll(HttpSession session) {
        Employee emp = (Employee) session.getAttribute("emp");
        List<Map<String,Object>> list = customerService.contractNumberByAll();
        System.out.println(list);
        return list;
    }

    //查询所有客户的合约统计
    @RequestMapping("/customer-contract-emp")
    @ResponseBody
    public  List<Map<String,Object>> contractNumberByEmp(HttpSession session) {
        Employee emp = (Employee) session.getAttribute("emp");
        List<Map<String,Object>> list = customerService.contractNumberByEmp(emp.getEmpId());
        System.out.println(list);
        return list;
    }

    //根据my客户生命周期查询所有数量
    @RequestMapping("/customer-lifecycle-number-my")
    @ResponseBody
    public List<Map<String,Object>> myCustomerNumberByLifeCycle(HttpSession session) {
        Employee emp = (Employee) session.getAttribute("emp");
        List<Map<String,Object>> list = customerService.myCustomerNumberByLifeCycle(emp.getEmpId());
        System.out.println(list);
        return list;
    }

    //根据my客户来源查询所有数量
    @RequestMapping("/customer-from-number-my")
    @ResponseBody
    public  List<Map<String,Object>> myCustomerNumberByFrom(HttpSession session) {
        Employee emp = (Employee) session.getAttribute("emp");
        List<Map<String,Object>> list = customerService.myCustomerNumberByFrom(emp.getEmpId());
        System.out.println(list);
        return list;
    }

    @RequestMapping("/new-customer")
    @ResponseBody
    public void newCustomer(@RequestBody Customer customer){
        Employee emp = (Employee) session.getAttribute("emp");
        customer.setEmployeeByEmpId(emp);
        customer.setCustomerLifecycle(1);
        customer.setCustomerType(1);
        System.out.println(customer);
        customerService.newCustomer(customer);
    }

    @RequestMapping("/update-customer")
    @ResponseBody
    public void updateCustomer(@RequestBody Customer customer){
        System.out.println(customer);
        customerService.updateCustomer(customer);
    }

    @RequestMapping("/update-customer-emp")
    @ResponseBody
    public void changeEmpAxios(@RequestBody List<Customer> changeEmpCustomerList){
        for (int i = 0; i< changeEmpCustomerList.size(); i++){
            customerService.updateCustomer(changeEmpCustomerList.get(i));
        }
    }
}
